//
//  TSMsgDatePickerTextFieldCell.m
//  chart
//
//  Created by xu on 15/8/2.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSMsgDatePickerTextFieldCell.h"

#define CELL_HEIGHT 50.f
#define TITLE_TEXT_FIELD_WIDTH 80.f

@implementation TSMsgDatePickerTextFieldCell

@synthesize titleLabel, contentTextField;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.userInteractionEnabled = YES;
        
        [self addTitleLabel];
        
        [self addTextField];
        
        [NOTIFICATIONCENTER addObserver:self selector:@selector(listenResignFirstResponder:) name:TSNotification_ResignFirstResponder object:nil];
    }
    
    return self;
}

- (void)setTitle:(NSString *)title
{
    if (titleLabel) {
        [titleLabel setText:title];
    }
    
    if (contentTextField) {
        //        contentTextField.inputAccessoryView = nil;
    }
}

- (void)addTitleLabel
{
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(MARGIN_LEFT, MARGIN_TOP, TITLE_TEXT_FIELD_WIDTH, CELL_HEIGHT-2*MARGIN_TOP)];
    [titleLabel setFont:FONT(12.f)];
    [self addSubview:titleLabel];
}

- (void)addTextField
{
    contentTextField = [[TSDatePickerTextField alloc] initWithFrame:CGRectMake(MARGIN_LEFT*2+TITLE_TEXT_FIELD_WIDTH, MARGIN_TOP, currentDeviceWidth-TITLE_TEXT_FIELD_WIDTH-MARGIN_LEFT*3, CELL_HEIGHT-2*MARGIN_TOP)];
    contentTextField.layer.borderWidth = 1.f;
    contentTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    [self addSubview:contentTextField];
}

- (void)removeAllSubviews
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)listenResignFirstResponder:(NSNotification *)noti
{
    if (contentTextField && [contentTextField isFirstResponder]) {
        [contentTextField resignFirstResponder];
    }
}

- (void)dealloc
{
    [NOTIFICATIONCENTER removeObserver:self];
}


@end
